/*
 * Copyright (c) 2011, Andrew Cole
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */

package net.andycole.xmiwriter;

import java.io.File;

import org.apache.log4j.Logger;

import org.eclipse.emf.ecore.resource.Resource;
//import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
//import org.eclipse.uml2.uml.resource.UML22UMLResource;
import org.eclipse.uml2.uml.resource.XMI2UMLResource;
import org.eclipse.uml2.uml.resource.UMLResource;
//import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;
import org.eclipse.emf.common.util.URI;
//import org.eclipse.emf.ecore.xmi.XMLResource;
import org.eclipse.uml2.uml.Model;

import net.andycole.xmiparser.model.IModel;


/**
 * Writes the contents of an EclipseModelImpl to an xmi file.
 * @author Andrew Cole
 */
public class EclipseEMFModelWriter implements IXMIWriter
{
   private File outputFile;
   private IModel model;

   private Logger LOG = Logger.getLogger(EclipseEMFModelWriter.class);

   public EclipseEMFModelWriter(File     outputFile,
                                IModel   model)
   {
      this.outputFile = outputFile;
      this.model = model;
   }

   /**
    * Write EMF-specific UML model content (as used in Eclipse)
    */
   @Override
   public void write()
   {
      try
      {
         //ResourceSet resourceSet = new ResourceSetImpl();
         ResourceSetImpl resourceSet;
         resourceSet = new ResourceSetImpl();

         //resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap()
			//	.put(XMI2UMLResource.FILE_EXTENSION, XMI2UMLResource.Factory.INSTANCE);

         //XMIResourceFactoryImpl
         resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap()
           .put(XMI2UMLResource.FILE_EXTENSION, UMLResource.Factory.INSTANCE);

//         String ext = UML22UMLResource.FILE_EXTENSION;
//         String xmiext = XMI2UMLResource.FILE_EXTENSION;
         // Make sure the output file ends with xmi or resource will not be
         // built.
         String destName = outputFile.toString();
         if (!destName.endsWith(".xmi"))
            destName += ".xmi";
         Resource resource = resourceSet.createResource(URI.createURI(destName));
         resource.getContents().add((Model)model.getBasePackage());

         resource.save(null);
      }
      catch (Exception error)
      {
//         System.err.println("Could not write to output File "+error.getMessage());
         LOG.error("Could not write to output File "+error.getMessage());
      }
   }

}
